\Grace's programme
\self control - multiple concurrent chain, dependant
\Red VI 30/VI30 initial link then FI2/FI2 terminal link with 1s rft on left, 4.5s rft on right
\Green VI30/VI30 initial link then terminal link, FI2 with 1s rft (left), FI28 with 4.5s rft (right)
\all terminal links made up to 60s total with ITI
\red and green alternate after every rft

\outputs
\^BR=1  \back light red  from Sarah's
\^BW=2  \back light white
\^BG=3  \back light green
^LR=4  \left light red
^LW=5  \left light white
^LG=6  \left light white
^RR=7  \right light red
^RW=8  \right light white
^RG=9  \right light green
^mag=10
^beep=11

\inputs
\R1=left key
\R2=right key
\R4=mag sensor

\event data codes..............................................

\first number = initial link key colour
\1=red
\2=green
\3=left white(red)
\4=right white (red)
\5=ITI (red)
\6=left white(green)
\7=right white (green)
\8=ITI (green)

\second number = event
\1=left peck
\2=right peck
\3=mag up
\4=mag down
\5=head in mag
\6=head out of mag
\7=lights on
\8=left blackout peck
\9=right blackout peck
\..................................................................

dim c=8000
dim e=29

\summary data
\e(0) - VIs      recording only so 30.30 = VI30 VI30
\e(1) - FI left red
\e(2) - FI right red
\e(3) - FI left green
\e(4) - FI right green
\e(5) - left rft length
\e(6) - right rft length
\----RED------------------------
\e(7) - pecks left
\e(8) - pecks right
\e(9) - time left
\e(10)- time right
\e(11)- FI pecks left
\e(12)- FI pecks right
\e(13)- rfts left
\e(14)- rfts right
\e(15)- black key - ITI pecks post red
\e(16)- black key - FI pecks
\----GREEN----------------------
\e(17)- pecks left
\e(18)- pecks right
\e(19)- time left
\e(20)- time right
\e(21)- FI pecks left
\e(22)- FI pecks right
\e(23)- rfts left
\e(24)- rfts right
\e(25)- black key - ITI pecks post green
\e(26)- black key - FI pecks
\--------------------------------
\e(27)- trials
\e(28)- total time
\e(29)- non-eat rfts

\VI lists
list L=7,9,15,19,24,26,30,32,43,45,51,59  \red left
list M=7,9,15,19,24,26,30,32,43,45,51,59  \red right
list N=7,9,15,19,24,26,30,32,43,45,51,59  \green left
list O=7,9,15,19,24,26,30,32,43,45,51,59  \green right

\variables

\a
\b - temp variables for codes, allows setting in advance
dim b=5
\b(0) - mag up
\b(1) - mag down
\b(2) - event code for head in mag
\b(3) - event code for head no longer in mag
\c - event data array
\d - used by background procedures
\e - summary data array
\f - current VI red left
\g - current VI red right
\h - current VI green left
\i - current VI green right
\j - ITI length
\k - counts ITI
\l - VI list for red left
\m - VI list for red right
\n - VI list for green left
\o - VI list for green right
\p - prop left pecks calc
\q - this FI length
\r - this rft length
\s
\t - time in tenths of seconds
\u
\v
\w
\x - current spot in c array
\y
\z

\z1 - end of session
\z2 - start of red
\z3 - start of green

\------------------------------------------------------------------------------------------
S.S.1,   \the main programme bit

s1,
  #start:show 1,Hen,animal number;
         set e(0)=30.30, \VIs
         e(1)=2,         \red left FI
         e(2)=2,         \red right FI
         e(3)=2,         \green left FI
         e(4)=28,        \green right FI
         e(5)=1,         \left rft length
         e(6)=4.5,        \right rft length
         c(x)=9999999;add x;
         randd f=L;     \first VI for left red
         randd g=m;     \& right red
         randd h=n;     \green left VI
         randd i=o;     \green right VI
         on ^LR,^RR;z2;
         set b(2)=15000000,b(3)=16000000,c(x)=17000000+t;add x;--->s2

\.....red bit.....................................................................

s2,  \concurrent
  #z1:off 1,2,3,4,5,6--->s1
  #R1:on^beep;set c(x)=11000000+t;
      add x,e(7);
      set p=e(7)/(e(7)+e(8));
      show 13,Pecks L,e(7),23,Prop Pecks,p;
      if f>0 [@cont,@done]
            @cont:--->s3
            @done:randd f=L;                          \get next VI
               set r=e(5)*1",q=e(1)*10,k=600;--->s4   \set rft and FI lengths
  #R2:on^beep;set c(x)=12000000+t;add x,e(8);
      set p=e(7)/(e(7)+e(8));
      show 18,Pecks R,e(8),23,Prop Pecks,p;
      if g>0 [@cont,@done]
            @cont:--->s3
            @done:randd g=m;set r=e(6)*1",q=e(2)*10,k=600;--->s5

s3,  \beep off, back to conc
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep;--->s2

s4,  \beep off, go to left chain
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep,^LR,^RR;on^LW;set b(2)=35000000,b(3)=36000000,c(x)=37000000+t;add x--->s6

s5,  \beep off, go to right chain
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep,^LR,^RR;on^RW;set b(2)=45000000,b(3)=46000000,c(x)=47000000+t;add x--->s8

s6,  \left chain (terminal link)
  #z1:off 1,2,3,4,5,6--->s1
  #R1:on^beep;set c(x)=31000000+t;add x,e(11);
      if q>0 [@cont,@rft]
         @cont:--->s7
         @rft:add e(13);show 33,Rft L,e(13);
              off^LW; set b(0)=33000000,b(1)=34000000;--->s10
  #R2:set c(x)=38000000+t;add x,e(16)--->sx

s7,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep--->s6

s8,  \right chain (terminal link)
  #z1:off 1,2,3,4,5,6--->s1
  #R2:on^beep;set c(x)=41000000+t;add x,e(12);
      if q>0 [@cont,@rft]
         @cont:--->s9
         @rft:add e(14);show 38,Rft R,e(14);
              off^RW; set b(0)=43000000,b(1)=44000000;--->s10
  #R1:set c(x)=48000000+t;add x,e(16)--->sx

s9,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep--->s8

\..red rft .....................................................
s10,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep;on^mag;
       set c(x)=b(0)+t;add x;--->s11

s11,
  #z1:off 1,2,3,4,5,6--->s1
  #R4:--->s12
  2":off^mag;set c(x)=b(1)+t;add x,e(29);
     show 50,non-Eat rfts,e(29);
     if e(29)>4 [@help,@OK]          \if 5 non-eat rfts sound the alarm and stop
         @help:alert on--->s14
         @OK:set b(2)=85000000,b(3)=86000000--->s13                 \go straight to ITI

s12,
  #z1:off 1,2,3,4,5,6--->s1
  r#T:off^mag;set b(2)=85000000,b(3)=86000000,c(x)=b(1)+t;add x,e(27);--->s13   \j=60-q,

s13,
  #z1:off 1,2,3,4,5,6--->s1
  #R1:set c(x)=51000000+t;add x,e(15)--->SX
  #R2:set c(x)=52000000+t;add x,e(15)--->SX
  0.1":if k>0 [@wait,@done]
           @wait:--->sx
           @done:on ^LG,^RG;z3;
                 set b(2)=25000000,b(3)=26000000,c(x)=27000000+t;add x;--->s22

\.....green bit.....................................................................

s22,   \concurrent
  #z1:off 1,2,3,4,5,6--->s1
  #R1:on^beep;set c(x)=21000000+t;
      add x,e(17);
      set p=e(17)/(e(17)+e(18));
      show 14,Pecks L,e(17),23,Prop Pecks,p;
      if h>0 [@cont,@done]
            @cont:--->s23
            @done:randd h=n;                          \get next VI
               set r=e(5)*1",q=e(3)*10,k=600;--->s24   \set rft and FI lengths
  #R2:on^beep;set c(x)=22000000+t;add x,e(18);
      set p=e(17)/(e(17)+e(18));
      show 19,Pecks R,e(18),24,Prop Pecks,p;
      if i>0 [@cont,@done]
            @cont:--->s23
            @done:randd i=o;set r=e(16)*1",q=e(4)*10,k=600;--->s25

s23,  \beep off, back to conc
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep;--->s22

s24,  \beep off, go to left chain
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep,^LG,^RG;on^LW;set b(2)=65000000,b(3)=66000000,c(x)=67000000+t;add x--->s26

s25,  \beep off, go to right chain
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep,^LG,^RG;on^RW;set b(2)=75000000,b(3)=76000000,c(x)=77000000+t;add x--->s28

s26,  \left chain (terminal link)
  #z1:off 1,2,3,4,5,6--->s1
  #R1:on^beep;set c(x)=61000000+t;add x,e(21);
      if q>0 [@cont,@rft]
         @cont:--->s27
         @rft:add e(23);show 34,Rft L,e(23);
              off^LW; set b(0)=63000000,b(1)=64000000;--->s30
  #R2:set c(x)=78000000+t;add x,e(26)--->sx

s27,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep--->s26

s28,  \right chain (terminal link)
  #z1:off 1,2,3,4,5,6--->s1
  #R2:on^beep;set c(x)=71000000+t;add x,e(22);
      if q>0 [@cont,@rft]
         @cont:--->s29
         @rft:add e(24);show 39,Rft R,e(24);
              off ^RW; set b(0)=73000000,b(1)=74000000;--->s30
  #R1:set c(x)=48000000+t;add x,e(16)--->sx

s29,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep--->s28

\.. green rft......................................................
s30,
  #z1:off 1,2,3,4,5,6--->s1
  0.05":off^beep;on^mag;
       set c(x)=b(0)+t;add x;--->s31

s31,
  #z1:off 1,2,3,4,5,6--->s1
  #R4:--->s32
  2":off^mag;set c(x)=b(1)+t;add x,e(29);
     show 50,non-Eat rfts,e(29);
     if e(29)>4 [@help,@OK]          \if 5 non-eat rfts sound the alarm and stop
         @help:alert on--->s14
         @OK:set b(2)=85000000,b(3)=86000000--->s15                 \go straight to ITI

s32,
  #z1:off 1,2,3,4,5,6--->s1
  r#T:off^mag;set b(2)=85000000,b(3)=86000000,c(x)=b(1)+t;add x,e(27);  \ j=60-q,
       show 40,Trials,e(27);
       if e(27)<48 [@cont,@stop]
                @cont:--->s15
                @stop:z1--->s1

s15,   \ITI
  #z1:off 1,2,3,4,5,6--->s1
  #R1:set c(x)=81000000+t;add x,e(25)--->SX
  #R2:set c(x)=82000000+t;add x,e(25)--->SX
  0.1":if k>0 [@wait,@done]
           @wait:--->sx
           @done:on ^LR,^RR;z2;
                 set b(2)=15000000,b(3)=16000000,c(x)=17000000+t;add x;--->s2

\........................................................................
s14,  \5 non-eat rfts
  0":show 22,5 non-eat rfts,0, 27,check hen,0, 32,and equip,0,
          37,play macro,0,42,"broken",0, 47,to save data,0 --->s16

s16,
  #K1:alert off;z1--->sx
\-------------------------------------------------------------------------------
S.S.2,  \timing stuff

s1,
  #start:--->s2

s2,
  #z1:set d=99;BKGRND 2;--->s3
  0.1":add t;sub k,q; set e(28)=e(28)+0.1;
       show 44,Total time,e(28),55,FI,q,60,k,k;
       if e(28)<3600 [@cont,@stop]
             @cont:--->sx
             @stop:set d=99;BKGRND 2;z1--->s3

s3,
  0.1":if d=1 [set c(x)=-987.987;]--->stopabortflush

\-------------------------------------------------------------------------------
S.S.3,   \VIs

s1,
  #start:--->s2

s2,
  #z2:show 15,red LVI,f,20,red RVI,g;--->s3
  #z3:show 15,red LVI,h,20,red RVI,i;--->s4

s3,  \red
  1":sub f,g; show 15,red LVI,f,20,red RVI,g;
     if (f <= 0) or (g <= 0) [@done,@cont]
        @done:--->s2
        @cont:--->sx

s4,  \green
  1":sub h,i; show 25,grn LVI,h,30,grn RVI,i;
     if (h <= 0) or (i <= 0) [@done,@cont]
        @done:--->s2
        @cont:--->sx

\-------------------------------------------------------------------------------
S.S.4,   \head in mag - event data only
         \code set as lights etc change through each trial
s1,
  #start:--->s2

s2,   \head not in mag, wait for beam break
  #R4:set c(x)=b(2)+t;add x;--->s3

s3,   \head in mag (beam broken), wait for no longer broken
  #R4:--->s3
  0.1":set c(x)=b(3)+t-1;add x;--->s2

\-------------------------------------------------------------------------------------------------













